import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import {userInfoStore} from "@/pinia/user";

const routes = [
 {
  path: '/',
  redirect: '/futures',
  name: 'Layout',
  component: () => import('@/Layout/index.vue'),
  children: [
   {
    path: 'futures',
    name: 'Futures',
    component:() => import('@/views/Futures/index.vue'),
    meta: {
     title: 'GLS - 智能期货交易系统',
     login: true
    }
   },
   {
    path: 'futureHistory',
    name: 'FutureHistory',
    component: () => import('@/views/Futures/History/index.vue'),
    meta: {
     title: 'GLS - 智能期货交易系统',
     login: true
    }
   }
  ]
 },
 {
  path: '/login',
  name: 'Login',
  component: () => import('@/views/Login/index.vue'),
  meta: {
   title: 'GLS - 登录',
   login: false
  }
 },
 {
  path: '/:pathMatch(.*)',
  //访问主页的时候 重定向到index页面
  redirect: '/futures',
 }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
 if (to.meta.title) document.title = to.meta.title

 const userInfo = userInfoStore()

 if (to.meta.login) {
  if (userInfo.getToken) {
   next()
  } else {
   next('/login')
  }
 } else {
  next()
 }
})

export default router
